package cn.zj.note;

import cn.zj.note.todo.Todo;
import cn.zj.note.todo.TodoManager;
import cn.zj.note.util.DateUtil;
import cn.zj.note.util.StrFmtUtil;
import cn.zj.note.view.RecordStage;
import cn.zj.note.view.TodoDetail;
import cn.zj.note.view.TodoListCell;
import cn.zj.note.view.event.AddTodoEventHandler;
import cn.zj.note.view.event.RecordEventHandler;
import javafx.application.Application;
import javafx.application.HostServices;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Pair;

import java.util.Optional;

public class MainApp extends Application {

    @FXML
    private static ListView<Todo> listView;
    @FXML
    private static TodoDetail todoDetail;
    public static HostServices hostServices;

    public static void main(String[] args) {
        TodoManager.getInstance();
        launch(args);
    }

    public static void setCurTodoDetail(TodoDetail curTodoDetail){
        todoDetail = curTodoDetail;
    }

    public static void refreshListView(){
        listView.refresh();
    }

    public void initialize() {
        listView = new ListView<>();
        listView.setPrefWidth(300);
        listView.setMinWidth(300);
        // 设置数据适配器
        ObservableList<Todo> items = FXCollections.observableArrayList(TodoManager.getInstance().getTodos());
        listView.setItems(items);

        // 设置自定义ListCell工厂
        Todo todo = null;
        if(!TodoManager.getInstance().getTodos().isEmpty()){
            todo = TodoManager.getInstance().getTodos().get(0);
        }
        todoDetail = new TodoDetail(todo);
        listView.setCellFactory(param -> new TodoListCell(todoDetail));
        todoDetail.update(todo);
    }

    @Override
    public void start(Stage primaryStage) {
        MainApp.hostServices = getHostServices();
        initialize();
        // 创建右键菜单
        ContextMenu contextMenu = new ContextMenu();
        // 创建菜单项
        TodoManager.getInstance().getResources().forEach(res->{
            MenuItem menuItemShowWindow = new MenuItem(res);
            menuItemShowWindow.setOnAction(event -> {
                todoDetail.getTodo().setResource(res);
                TodoManager.getInstance().saveData(todoDetail.getTodo());
                refreshListView();
            });
            // 将菜单项添加到右键菜单
            contextMenu.getItems().addAll(menuItemShowWindow);
        });
        listView.setContextMenu(contextMenu);
        // 右键点击时显示菜单
        listView.setOnContextMenuRequested(event -> {
            contextMenu.show(listView, event.getScreenX(), event.getScreenY());
        });
        MenuBar menuBar = getMenuBar();
        // 创建任务详情区域
        HBox contentBox = new HBox(listView, todoDetail);
        HBox.setHgrow(todoDetail, Priority.ALWAYS);

        BorderPane root = new BorderPane();
        root.setTop(menuBar);
        root.setCenter(contentBox);

        Scene scene = new Scene(root, 1000, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private static MenuBar getMenuBar() {
        // 创建菜单栏
        MenuBar menuBar = new MenuBar();
        Menu newTodoMenu = new Menu("任务");
        Menu dataMenu = new Menu("报表");
        menuBar.getMenus().addAll(newTodoMenu,dataMenu);

        // 添加新任务按钮
        MenuItem addTaskButton = new MenuItem("新建任务");
        addTaskButton.setOnAction(new AddTodoEventHandler(listView));
        // 资源管理
        MenuItem resBtn = new MenuItem("资源管理");
        resBtn.setOnAction(event -> {

        });
        newTodoMenu.getItems().addAll(addTaskButton, resBtn);
        // 报表
        String title = "昨日报";
        MenuItem lastDailyData = new MenuItem(title);
        title = "日报";
        lastDailyData.setOnAction(new RecordEventHandler(title, RecordEventHandler.TimeEnum.LAST_DAY));
        MenuItem dailyData = new MenuItem(title);
        title = "周报";
        dailyData.setOnAction(new RecordEventHandler(title, RecordEventHandler.TimeEnum.CUR_DAY));
        MenuItem weekData = new MenuItem(title);
        weekData.setOnAction(new RecordEventHandler(title, RecordEventHandler.TimeEnum.CUR_WEEK));
        title = "半年报";
        MenuItem yearData = new MenuItem(title);
        yearData.setOnAction(new RecordEventHandler(title, RecordEventHandler.TimeEnum.CUR_HALF_YEAR));
        dataMenu.getItems().addAll(lastDailyData, dailyData,weekData,yearData);
        return menuBar;
    }
}